--本末倒置
local benmodaozhi = fk.CreateSkill {
    name = "mcdh_benmodaozhi",
}

local U = require "packages/utility/utility"
local lib = require "packages/mcdh/lib"

benmodaozhi:addEffect(fk.EventPhaseStart,{
    can_trigger = function(self, event, target, player, data)
        return player:hasSkill(self) and target == player and player.phase == Player.Start and
                #table.filter(player.room:getOtherPlayers(player), function(p) return (player:canUseTo(Fk:cloneCard("duel"),p)) end) > 0
    end,
    on_cost = function (self, event, target, player, data)
        local room = player.room
        local useduel_data = U.askForUseVirtualCard(room, player, "duel", nil, self.name, "#mcdh_benmodaozhi_1",nil,nil,nil,false,nil,true)
        if useduel_data then
            self.cost_data = useduel_data
            return true
        end
    end,
    on_use = function(self, event, target, player, data)
        local room = player.room
        local use = self.cost_data
        room:useCard(use)
        while use and use.damageDealt and use.damageDealt[player.id] do
            if player.dead then
                break
            end
            room:addPlayerMark(player,"@#mcdh_benmodaozhi_run",1)
            if #table.filter(room:getOtherPlayers(player), function(p) return (player:canUseTo(Fk:cloneCard("duel"),p)) end) == 0 then
                break
            end
            use = U.askForUseVirtualCard(room, player, "duel", nil, self.name, "#mcdh_benmodaozhi_2",false,nil,nil,false,nil,true)
            if use then
                room:useCard(use)
            end
        end
    end,
})

benmodaozhi:addEffect(fk.TurnStart,{
    can_refresh = function (self, event, target, player, data)
        return target == player
    end,
    on_refresh = function (self, event, target, player, data)
        local room = player.room
        room:setPlayerMark(player,"@#mcdh_benmodaozhi_run",0)
    end
})

benmodaozhi:addEffect("distance",{
    correct_func = function(self, from, to)
        return to:getMark("@#mcdh_benmodaozhi_run")
    end,
})

Fk:loadTranslationTable {
    ["mcdh_benmodaozhi"] = "舛逆",
    [":mcdh_benmodaozhi"] = "准备阶段，你可以视为使用一张【决斗】，若你以此法受到伤害，直到你的回合开始你与其他角色计算距离+1，然后重复此流程。",
    ["#mcdh_benmodaozhi_1"] = "舛逆：你可以视为使用一张【决斗】",
    ["#mcdh_benmodaozhi_2"] = "舛逆：你视为使用一张【决斗】",
    ["@#mcdh_benmodaozhi_run"] = "舛",
}

return benmodaozhi